package api

import (
	"anban/models"
	"anban/service"
	"anban/utils"
)

type IndexController struct {
	BaseController
}

// 获取最新通知
func (c *IndexController) NewNotice() {
	var res *utils.ResJsonStruct
	var p map[string]interface{}

	userId := c.GetSession("miniUserId")
	p = map[string]interface{}{}
	p["id"] = userId
	user := service.GetUserInfo(p)

	var studentIds []int64
	p = map[string]interface{}{}
	p["status"] = 1
	p["familySn"] = user.FamilySn
	_, studentList := service.GetStudentList(p)
	for _, student := range studentList {
		studentIds = append(studentIds, student.Id)
	}

	var newNoticeList []*models.NewNotice
	if len(studentIds) > 0 {
		p = map[string]interface{}{}
		p["relation"] = true
		p["api"] = true
		p["limit"] = 10
		p["studentIds"] = studentIds
		_, newNoticeList = service.GetNewNoticeList(p)
	}

	res = utils.ResJson(0, "成功获取数据", newNoticeList)
	c.Data["json"] = res
	c.ServeJSON()
	return
}

// 老师所带班级列表
func (c *IndexController) ClassListByTeacher() {
	var res *utils.ResJsonStruct
	var p map[string]interface{}

	userId := c.GetSession("miniUserId")
	p = map[string]interface{}{}
	p["userId"] = userId
	p["status"] = 1
	_, classList := service.GetClassList(p)

	res = utils.ResJson(0, "成功获取数据", classList)
	c.Data["json"] = res
	c.ServeJSON()
	return
}

// 某个班级信息
func (c *IndexController) ClassInfoByTeacher() {
	var res *utils.ResJsonStruct
	var p map[string]interface{}
	classId := c.GetString("id")

	if len(classId) <= 0 {
		res = utils.ResJson(1300, "班级Id异常", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	p = map[string]interface{}{}
	p["id"] = classId
	p["status"] = 1
	p["apiWithStudent"] = true
	classInfo := service.GetClassInfo(p)

	res = utils.ResJson(0, "成功获取数据", classInfo)
	c.Data["json"] = res
	c.ServeJSON()
	return
}

// 科目列表
func (c IndexController) CourseList() {
	var res *utils.ResJsonStruct
	var p map[string]interface{}

	p = map[string]interface{}{}
	p["status"] = 1
	_, courseList := service.GetCourseList(p)

	res = utils.ResJson(0, "成功获取数据", courseList)
	c.Data["json"] = res
	c.ServeJSON()
	return
}

// 获取省列表
func (c *IndexController) ProvinceList() {
	var res *utils.ResJsonStruct
	var p map[string]interface{}

	p = map[string]interface{}{}
	p["status"] = 1
	p["level"] = 1
	p["parentId"] = 0
	_, provinceList := service.GetRegionList(p)

	res = utils.ResJson(0, "成功获取数据", provinceList)
	c.Data["json"] = res
	c.ServeJSON()
	return
}

// 按省查找城市列表
func (c *IndexController) CityListByProvince() {
	var res *utils.ResJsonStruct
	var p map[string]interface{}
	provinceId := c.GetString("id")

	p = map[string]interface{}{}
	p["status"] = 1
	p["level"] = 2
	p["parentId"] = provinceId
	_, cityList := service.GetRegionList(p)

	res = utils.ResJson(0, "成功获取数据", cityList)
	c.Data["json"] = res
	c.ServeJSON()
	return
}

// 按市查找区域列表
func (c *IndexController) DistrictListByCity() {
	var res *utils.ResJsonStruct
	var p map[string]interface{}
	cityId := c.GetString("id")

	p = map[string]interface{}{}
	p["status"] = 1
	p["level"] = 3
	p["parentId"] = cityId
	_, districtList := service.GetRegionList(p)

	res = utils.ResJson(0, "成功获取数据", districtList)
	c.Data["json"] = res
	c.ServeJSON()
	return
}

// 按区域查找学校列表
func (c *IndexController) SchoolListByDistrict() {
	var res *utils.ResJsonStruct
	var p map[string]interface{}
	districtId := c.GetString("id")

	p = map[string]interface{}{}
	p["status"] = 1
	p["districtId"] = districtId
	_, schoolList := service.GetSchoolList(p)

	res = utils.ResJson(0, "成功获取数据", schoolList)
	c.Data["json"] = res
	c.ServeJSON()
	return
}

// 按学校查找班级
func (c *IndexController) ClassListBySchool() {
	var res *utils.ResJsonStruct
	var p map[string]interface{}
	schoolId := c.GetString("id")

	p = map[string]interface{}{}
	p["status"] = 1
	p["schoolId"] = schoolId
	_, classList := service.GetClassList(p)

	res = utils.ResJson(0, "成功获取数据", classList)
	c.Data["json"] = res
	c.ServeJSON()
	return
}